home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / swap_endian.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  69 lines

  1. ; $Id: swap_endian.pro,v 1.8 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1993-1997, Research Systems, Inc. All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;    SWAP_ENDIAN
  8. ;
  9. ; PURPOSE:
  10. ;    This fucntion reverses the byte ordering of arbitrary scalars,
  11. ;    arrays or structures. It may be used, for example, to make little
  12. ;    endian numbers big, or big endian numbers little.
  13. ;
  14. ; CATEGORY:
  15. ;    Utility.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;    Result = SWAP_ENDIAN(A)
  19. ;
  20. ; INPUTS:
  21. ;    A:    The scalar, array, or structure to be swapped.
  22. ;
  23. ; OUTPUTS:
  24. ;    Result:    The same type and structure as the input, with the
  25. ;        pertinent bytes reversed.
  26. ;
  27. ; PROCEDURE:
  28. ;    Swap arrays and scalars directly using BYTEORDER.
  29. ;    Swap structures recursively.
  30. ;
  31. ; EXAMPLE:
  32. ;    A = SWAP_ENDIAN(A)  ;Reverses the "endianness" of A
  33. ;
  34. ; MODIFICATION HISTORY:
  35. ;    DMS, RSI, May, 1993.    Written.
  36. ;    DMS, RSI, July, 1993.   Added double complex.
  37. ;-
  38.  
  39. function swap_endian, in
  40. t = in            ;Make a copy
  41. s = size(t)
  42. case s[s[0]+1] of     ;Type code
  43. 1: return, t        ;don't change bytes
  44. 2: byteorder, t, /SSWAP  ;shorts
  45. 3: byteorder, t, /LSWAP  ;Longs
  46. 4: byteorder, t, /LSWAP  ;single floats
  47. 5: BEGIN        ;Double, swap longs & then swap even/odds
  48.    n = n_elements(t)
  49.    dims = size(in)    ;Preserve dimensions.
  50.    t = long(temporary(t), 0, 2 * n)  ;Cvt to longs
  51.    byteorder, t, /LSWAP   ;swap each long
  52.    t = t[lindgen(2*n) xor 1L]  ;swap pairs
  53.    t = double(temporary(t),0,n)
  54.    if dims[0] gt 0 then $    ;Return array?
  55.     return, reform(t, dims[1:dims[0]], /overwrite)
  56.    return, t[0]        ;Return scalar
  57.    ENDCASE
  58. 6: byteorder, t, /LSWAP  ;Complex
  59. 7: return, t        ;String
  60. 8: for i=0, n_tags(t)-1 do begin
  61.     temp = swap_endian(t.(i))
  62.     t.(i) = temp
  63.     endfor
  64. 9: byteorder, t, /LSWAP    ;Double complex.
  65. ENDCASE
  66. return, t
  67. end
  68.  
  69.